/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.system.service.mapstruct;

import com.fzhucc.modules.system.domain.User;
import com.fzhucc.modules.system.service.dto.UserDto;
import com.fzhucc.modules.system.service.impl.MsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zheng Jie
 * @date 2018-11-23
 */
public abstract class UserMapperDecorator implements UserMapper {

    @Autowired
    private MsUserService msUserService;

    @Autowired
    @Qualifier("delegate")
    private UserMapper userMapper;

    @Override
    public UserDto toDto(User entity) {
        UserDto user = userMapper.toDto(entity);
        com.fzhucc.ms.auth.domain.dto.UserDto msUser = msUserService.findByUsername(user.getUsername());
        if (msUser != null) {
            user.setPhone(msUser.getPhone());
            user.setPassword(msUser.getPassword());
            user.setAuths(msUser.getAuths());
        }
        return userMapper.toDto(entity);
    }

    @Override
    public List<UserDto> toDto(List<User> entityList) {
        if (entityList == null) {
            return null;
        }
        List<UserDto> list = new ArrayList<UserDto>(entityList.size());
        for (User user : entityList) {
            list.add(toDto(user));
        }
        return list;
    }
}
